<?php

session_start();

include_once '../Model/DAL/Login.php';
include_once '../Model/DAL/Functions.php';

class loginController {

    public $model;

    function __construct() {
        $this->model = new Login();
    }

    function loginPopup() {
        include_once 'templates/dialog/login-popup.php';
    }

    //Player
    function checkLogin() {
        $userLog = $this->model->checkLogin();
        if ($userLog != NULL) {
            $_SESSION['userLog'] = $userLog;
        }
        header("Location: index.php");
    }

    //Agent
    function checkLoginAgent() {
        $msg = '';
        $agentLog = $this->model->checkLoginAgent();
        if ($agentLog != NULL) {
            if ($agentLog['isLocked'] == 'N') {
                $_SESSION['agentLog'] = $agentLog;
                header("Location: agent-management-taikhoan.php");
            } else {
                //Người dùng này đã bị khóa
                $msg = 'Y';
                header("Location: login-agent.php?msg=" . $msg);
            }
        } else {
            //Tên người dùng hoặc mật khẩu sai !
            $msg = 'S';
            header("Location: login-agent.php?msg=" . $msg);
        }
    }

    //Player
    function logout() {
        if (isset($_SESSION['userLog'])) {
            unset($_SESSION['userLog']);
            header("Location: index.php");
        }
    }

    function getSessionLogin() {
        return (isset($_SESSION['userLog'])) ? $_SESSION['userLog'] : NULL;
    }

    //Player
    function logoutAgent() {
        if (isset($_SESSION['agentLog'])) {
            unset($_SESSION['agentLog']);
            header("Location: login-agent.php");
        }
    }

    function getSessionLoginAgent() {
        return (isset($_SESSION['agentLog'])) ? $_SESSION['agentLog'] : NULL;
    }

    function getPlayerLogonInfor() {
        $userLog = $this->getSessionLogin();
        $playerID = $userLog["PlayerID"];
        $userLogInfor = $this->model->getPlayerLogonInfor($playerID);
        return $userLogInfor;
    }

    function getPlayerBetHistory() {
        $userLog = $this->getSessionLogin();
        $playerID = $userLog["PlayerID"];
        $result = $this->model->getPlayerBetHistory($playerID);
        include_once 'templates/profile/tmp-table-player-bet-history.php';
    }

    function checkOldPasswordPlayer() {
        $userLog = $this->getSessionLogin();
        $playerID = $userLog["PlayerID"];
        $result = $this->model->checkOldPasswordPlayer($playerID);
        return $result;
    }

    function updateNewPasswordPlayer() {
        $userLog = $this->getSessionLogin();
        $playerID = $userLog["PlayerID"];
        $result = $this->model->updateNewPasswordPlayer($playerID);
        return $result;
    }

}
